package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xn.app.muying.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.ToastUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2016/10/19.
 */
public class YunQiXuanZeActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private String result;
    private int id;
    private String pageIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yunqixuanze_activity);
        ImageView back = (ImageView) findViewById(R.id.yunqixuanze_activity_back);
        btn1 = (Button) findViewById(R.id.yunqixuanze_activity_btn1);
        btn2 = (Button) findViewById(R.id.yunqixuanze_activity_btn2);
        back.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        pageIntent = getIntent().getStringExtra("PAGE_INTENT");
    }

    @Override
    public void onClick(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.yunqixuanze_activity_back:
                finish();
                break;

            case R.id.yunqixuanze_activity_btn1:
                updateInterfaceData();
                break;

            case R.id.yunqixuanze_activity_btn2:
                /**
                 * PAGE_INTENT:1、HomeFragment;2、MineFragment(跳转页面提示);
                 * SIGN：1、continue;2、return (医院Acticity是否再次跳转提示);
                 */
                if (pageIntent != null) {
                    intent.putExtra("SIGN", "continue");
                    intent.putExtra("PAGE_INTENT", pageIntent);
                    intent.setClass(this, JiuZhenActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(this, "没有拿到pageInent的值", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void updateInterfaceData() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("pregnancyStatus", 0 + "");

                    String p = new Gson().toJson(param);
                    Log.i("allen==", p);

                    String tokens = Constants.getStringByName(YunQiXuanZeActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), p);
                    Request request = new Request.Builder().url("http://apitest.beierbaby.com/api/account/pregnancy-info").addHeader("Authorization", "Bearer " + token.get("access_token")).put(formBody).build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    int code = response.code();
                    String bodys = response.body().string();

                    Log.i("code=+=", "code==" + code);
                    Log.i("bodys=+=", bodys);
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (pageIntent != null) {
                                    Intent intent = new Intent();
                                    intent.setAction("PREPARE_PREGNANCY_INFO");
                                    intent.putExtra("PAGE_INTENT", pageIntent);
                                    sendBroadcast(intent);
                                } else {
                                    Toast.makeText(YunQiXuanZeActivity.this, "没有拿到PAGE_INTENT的值", Toast.LENGTH_SHORT).show();
                                }
                                finish();
                            }
                        });
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "修改成功");

                    break;
                case 1:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "修改失败");

                    break;
            }
        }
    };

}
